package com.property.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.property.biz.LrrelationBiz;
import com.property.entity.Lrrelation;
import com.property.entity.Management;

public class nvcController implements HandlerInterceptor{

	@Autowired 
	private LrrelationBiz lrrelationBiz;
	
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		boolean foo=true;
		String requestUri = request.getRequestURI();
        String contextPath = request.getContextPath();
        String url = requestUri.substring(contextPath.length());
        System.out.println(requestUri);
        System.out.println(url);
        
        System.out.println("-"+contextPath);
        int i=0;
        switch (url) {
        case "/main/houselist":
        	i=1;
        	break;
        case "/main/complaa":
        case "/main/reportdetail":
        	i=2;
        	break;
		case "/main/complaint":
		case "/main/detail":
			i=3;
			break;
		case "/main/informlist":
			i=4;
			break;
		case "/main/inform":
			i=4;
			break;
		case "/main/community":
			i=5;
			break;
		case "/main/compilewrite":
			i=6;
			break;
		default:
			i=-1;
			break;
		}
       
        Management management=(Management) request.getSession().getAttribute("management");
        List<Lrrelation> list=lrrelationBiz.getLimit(management.getId());
        if(i==-1){
        	return true;
        }
        if(management!=null){
        if(list.size()>0 ){
        	for(Lrrelation lrrelation:list){
            	System.out.println(lrrelation.getLimit().getId());
            	if(lrrelation.getLimit().getId()==i){
            		foo=true;
            		break;
            	}
            	foo=false;
            }
        }else{
        	foo=false;
        }
        
        }
        if(foo==false){
        	response.sendRedirect("/property/1031");
        	return foo;
        }
		return true;
		
	}

	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		
		
	}

	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

}
